package com.yunxi.service.dubboService.impl.sat;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.DictConstant;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.model.sat.Evaluate;
import com.yunxi.sat.pojo.EvaluateCountVO;
import com.yunxi.sat.pojo.EvaluateVO;
import com.yunxi.sat.service.EvaluateDubboService;
import com.yunxi.service.localService.sat.EvaluateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

/**
 * <p>
 * 满意度表 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-09-26
 */
public class EvaluateDubboServiceImpl implements EvaluateDubboService {

    @Autowired
    @Qualifier(value = "evaluateServiceImpl")
    EvaluateService evaluateService;


    @Override
    public EvaluateVO getById(String id) throws Exception {
        Evaluate evaluate = evaluateService.getById(id);
        if(evaluate == null)throw new Exception("操作失败,无法根据主键:"+id+"找到对应记录!");
        EvaluateVO evaluateVO = new EvaluateVO();
        BeanUtils.copyProperties(evaluate, evaluateVO);
        return evaluateVO;
    }

    @Override
    public void saveOrUpdate(EvaluateVO evaluateVO) throws Exception {
        Evaluate evaluate = new Evaluate();
        BeanUtils.copyProperties(evaluateVO, evaluate);
        evaluateService.saveOrUpdate(evaluate);
    }

    @Override
    public void removeById(String id) {
        evaluateService.removeById(id);
    }

    @Override
    public PageInfo<EvaluateVO> findPageByVO(EvaluateVO evaluateVO, Integer pageNum, Integer pageSize, LoginInfo loginInfo) throws Exception {
        String roleCode = loginInfo.getRoleCode();
        //销售顾问看自己的
        if(StringUtils.isNotEmpty(roleCode) && roleCode.equals(DictConstant.USER_ROLE_SALES)){
            evaluateVO.setSalesConsultantNo(loginInfo.getUserCode());
        }
        //销售经理看全店
        else if(StringUtils.isNotEmpty(roleCode) && roleCode.equals(DictConstant.USER_ROLE_MANAGER)){
            evaluateVO.setPartnerCode(loginInfo.getPartnerCode());
        }
        PageInfo<EvaluateVO> pageInfo = evaluateService.pageByVo(evaluateVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<EvaluateVO> evaluateVOList) throws Exception {
        evaluateService.removeByListVo(evaluateVOList);
    }

    @Override
    public List listByVo(EvaluateVO evaluateVO) {
        return evaluateService.listByVo(evaluateVO);
    }

    /**
     * 满意度统计
     * @param loginInfo
     * @return
     * @throws Exception
     */
    @Override
    public  List<List<EvaluateCountVO>> evaluateCount(LoginInfo loginInfo) throws Exception {
        String roleCode = loginInfo.getRoleCode();
        EvaluateVO evaluateVO = new EvaluateVO();
        //销售顾问查自己
        if(StringUtils.isNotEmpty(roleCode) && roleCode.equals(DictConstant.USER_ROLE_SALES)){
            evaluateVO.setSalesConsultantNo(loginInfo.getUserCode());
        }
        //销售经理查全店
        else if(StringUtils.isNotEmpty(roleCode) && roleCode.equals(DictConstant.USER_ROLE_MANAGER)){
            evaluateVO.setPartnerCode(loginInfo.getPartnerCode());
        }
        return evaluateService.evaluateCount(evaluateVO);
    }
}
